﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.Collections;
using System.Configuration;
using System.Configuration.Assemblies;

using System.Collections.Specialized;

using System.Runtime.InteropServices;

namespace etimet
{
    public partial class OptionsForm : Form
    {
        public OptionsForm()
        {

            InitializeComponent();

            this.StartPosition = FormStartPosition.CenterScreen;

            TranslationHelper.TranslateForm(Program.conf.CurrentLang, this);

            foreach (string s in AppConfiguration.SupportedLanguages.Split(','))
            {
                // get language name
                ResourceManager rm = new ResourceManager("etimet.i18nResources.i18n", Assembly.GetExecutingAssembly(), null);
                string line = s + " - " + rm.GetString("LangName", new CultureInfo(s));
                this.LangComboBox.Items.Add(line);

                // set current lang
                this.LangComboBox.SelectedIndex = LangComboBox.FindString(Program.conf.CurrentLang);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void StoreConfig()
        {
            Program.conf.StoreConfigOption("CurrentLanguage", this.LangComboBox.SelectedItem.ToString().Split('-')[0].Trim());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // compare to new language, because maybe the user want's immidiate replacement!
            string newLang = this.LangComboBox.SelectedItem.ToString().Split('-')[0].Trim();
            if (!Program.conf.CurrentLang.Equals(newLang))
            {
                DialogResult a = MessageBox.Show(
                    TranslationHelper.TranslateString("ChangeLanguageRestartConfirmaionText", newLang),
                    TranslationHelper.TranslateString("ChangeLanguageRestartConfirmaionTitle", newLang),
                    MessageBoxButtons.YesNoCancel,
                    MessageBoxIcon.Question,
                    MessageBoxDefaultButton.Button1);
                if (a == DialogResult.Yes) {
                    StoreConfig();

                    // all of these steps are a must for succesfull restart
                    Program.mainform.notifyIcon1.Dispose();
                    Program.mainform.Dispose();
                    Application.Restart();
                }
                else if (a == DialogResult.No) {
                    StoreConfig();
                }
                else {
                    return;
                }
            }
            this.Dispose();
        }

        private void OptionsForm_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void OptionsForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Application.ExitThread();
        }

        private void OptionsForm_Load(object sender, EventArgs e)
        {
        }

    }
}
